/**
 * Created by admin on 2016/12/3.
 */

'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.post('/user/err', async ctx => {
  return ctx.msg = "ok";
}));

// app.use(app.routes.post('/user/login', async function () {
//   let token = this.req.json.kToken;
//
//   var str = await http.get(app.appConfig.fontHost + "api/member/get", {headers: {token: "1"}});
//   try {
//     let id = JSON.parse(str).data.id;
//
//     let user = dbr.find("select * from thwy_member where id=:id", {id: id});
//     this.session.user = user;
//   }
//   catch (e) {
//     this.msg = e.message;
//   }
// }));

app.use(app.routes.get('/user/test', async function () {
  this.body = {v: "!"};
  if (this.session.user) {
    this.body = this.session.user;
  }
}));

app.use(app.routes.post('/user/info', async function () {
  if (this.session.user) {
    this.body = jv.extend({}, this.session.user, {type: this.session.user_type});
  }
}));

app.use(app.routes.post('/user/getAddresses', async function () {
  let userId = this.needLogin();
  if (!userId) {
    return;
  }
  let store = await dbr.find(`select id addressId ,detailed_address address,is_default isDefault
from thwy_member_address  
where member_id = :id
limit 0,20
`, {id: userId});

  this.body = store;
}));
